package com.lancoo.answer.view.base

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.widget.RelativeLayout
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.lancoo.answer.R
import com.lancoo.answer.util.DensityUtils

abstract class BaseVMActivity<DB : ViewDataBinding> : AppCompatActivity() {

    lateinit var mBinding: DB

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onCreate(savedInstanceState: Bundle?) {
        mBinding = DataBindingUtil.setContentView(this, setLayoutID())
        initView()
        initData()
        fullScreen(false)
        super.onCreate(savedInstanceState)
    }

    fun fullScreen(dark: Boolean) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val decorView = window.decorView
            if (dark) {
                decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            } else {
                decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            }
            window.statusBarColor = Color.TRANSPARENT
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        } else {
            val attributes = window.attributes
            val flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            attributes.flags = attributes.flags or flagTranslucentStatus
            window.attributes = attributes
        }
        val mRlTopBar = findViewById<RelativeLayout>(R.id.rl_toolbar)
        setBarPadding(mRlTopBar)
    }

    private fun setBarPadding(view: View) {
        view.setPadding(
            0,
            getStatusBarHeight(),
            0,
            view.paddingBottom + DensityUtils.dip2px(6f, this)
        )
    }

    private  fun getStatusBarHeight(): Int {
        val resources = this.resources
        val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
        return resources.getDimensionPixelSize(resourceId)
    }


    abstract fun setLayoutID(): Int
    abstract fun initView()
    open fun initData() {}

}